package cn.rkylin.oms.warehouse.shiporder.adapter;

import cn.rkylin.apollo.common.util.HttpUtils;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import cn.rkylin.oms.warehouse.shiporder.request.ConsignShipOrderRequest;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component("suningShiporderAdapter")
public class SuningShiporderAdapter extends ShiporderAdapter {

    @Value("${gateway}")
    private String gateway;

    @Autowired
    LogUtil logUtil;

    /**
     * 平台发货
     *
     * @param shopId
     * @param consignShipOrderRequest
     * @return
     * @throws Exception
     */
    @Override
    public String platfromConsign(String shopId, ConsignShipOrderRequest consignShipOrderRequest) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> validateMap = super.pushDataValidate(shopId);

        if (validateMap.get("success").equals("y")) {

            ShopVO shop = (ShopVO) validateMap.get("shop");
            String requestParams = JSON.toJSONString(consignShipOrderRequest);
            String requestUrl = gateway + "/microservice-suning/ship/consignSuningShipOrder?shopId=" + shop.getShopId();
            String resJson = HttpUtils.sendByPost(requestUrl, requestParams);
            Map<String, String> resultMap = JSON.parseObject(resJson, Map.class);
            // 记录日志
            logUtil.info(new DBLog(consignShipOrderRequest.getEcTradeCode(), "consignSuningShipOrder", "platfromConsign",
                    "[" + requestParams + "," + resultMap.get("msg") + "]"));
            if ("true".equals(resultMap.get("success"))) {
                returnMap.put("success", "y");
                returnMap.put("msg", "");
            } else {
                returnMap.put("success", "n");
                returnMap.put("msg", resultMap.get("msg"));
            }
            return JSON.toJSONString(returnMap);
        }
        return JSON.toJSONString(returnMap);
    }
}
